#include "./include/PDF2BinExport.h"
#include <iostream>
#include <string.h> 
#include <stdlib.h>
using namespace std;

#include <stdio.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <dirent.h> 

int is_dir(char * filename)
{
	struct stat buf;
	int ret = stat(filename,&buf);
	if(0 == ret)
	{
		if(buf.st_mode & S_IFDIR)
		{
		//printf("%s is folder\n",filename);
		return 0;
		}
		else
		{
			//printf("%s is file\n",filename);
			return 1;
		}
	}
	return -1;
}

int delete_dir(char * dirname)
{
	char chBuf[256];
	DIR * dir = NULL;
	struct dirent *ptr;
	int ret = 0;
	dir = opendir(dirname);
	if(NULL == dir)
	{
		return -1;
	}
	while((ptr = readdir(dir)) != NULL)
	{
		ret = strcmp(ptr->d_name, ".");
		if(0 == ret)
		{
			continue;
		}
		ret = strcmp(ptr->d_name, "..");
		if(0 == ret)
		{
			continue;
		}
		snprintf(chBuf, 256, "%s/%s", dirname, ptr->d_name);
		ret = is_dir(chBuf);
		if(0 == ret)
		{
			ret = delete_dir(chBuf);
			if(0 != ret)
			{
				return -1;
			}
		}
		else if(1 == ret)
		{
			ret = remove(chBuf);
			if(0 != ret)
			{
				return -1;
			}
		}
	}
	(void)closedir(dir);

	ret = remove(dirname);
	if(0 != ret)
	{
		return -1;
	}
	return 0;
}

int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8")  
{  
	if (src == NULL) 
	{  
		dest = NULL;  
		return 0;  
	}  

	// 根据环境变量设置locale  
	setlocale(LC_CTYPE, locale);  

	// 得到转化为需要的宽字符大小  
	int w_size = mbstowcs(NULL, src, 0) + 1;  

	// w_size = 0 说明mbstowcs返回值为-1。即在运行过程中遇到了非法字符(很有可能使locale  
	// 没有设置正确)  
	if (w_size == 0) 
	{  
		dest = NULL;  
		return -1;  
	}  

	//wcout << "w_size" << w_size << endl;  
	dest = new wchar_t[w_size];  
	if (!dest) 
	{  
		return -1;  
	}  

	int ret = mbstowcs(dest, src, strlen(src)+1);  
	if (ret <= 0)
	{  
		return -1;  
	}  
	return 0;  
}  

char * left(char *dst,char *src, int n)
{
	char *p = src;
	char *q = dst;
	int len = strlen(src);
	if(n>len) n = len;
	while(n--) *(q++) = *(p++);
	*(q++)='\0';
	return dst;
}

char * right(char *dst,char *src, int n)
{
	char *p = src;
	char *q = dst;
	int len = strlen(src);
	if(n>len) n = len;
	p += (len-n);   /*从右边第n个字符开始*/
	while(*(q++) = *(p++));
	return dst;
}

int main(int argc, char** argv)
{
	//FX_INT32 nAnnotCount = 1;
	PDF2BinMouldeMgr mgr = CreateMoudleMgr();
	PD2BinConvertor cvt = NewConvertor();

	char* csSrcPath = "./foxiTest/";
	char* csDestPath = "./foxiTest/";
	DIR* dir;
	struct dirent* drt;

	if((dir = opendir(csSrcPath)) == NULL)
	{	
		cout << "NULL Path....." << endl;
		return 0;
	}

	int nPDFFileCount = 0;
	//char *fileName[100];
	string strFileName[100];
	while((drt = readdir(dir)) != NULL)
	{
		if(strcmp(drt->d_name, ".") == 0 || strcmp(drt->d_name, "..") == 0)
			continue;

		char dst[10];
		right(dst, drt->d_name, 4);
		if(strcmp(".pdf", dst) == 0)
		{
			char cFileName[100];
			left(cFileName, drt->d_name, strlen(drt->d_name)-4);
			strFileName[nPDFFileCount] = cFileName;
			nPDFFileCount ++;
		}
	}
	closedir(dir);

	for(int i = 0; i < nPDFFileCount; i++)
	{
		string sPath = csSrcPath;
		sPath = sPath + strFileName[i] + ".pdf";
		char* sSrcPath = const_cast<char*>(sPath.c_str());
		wchar_t* wsSrcPath;
		ToWchar(sSrcPath, wsSrcPath);
		//wcout << wsSrcPath << " +++++  " << i << endl;

		sPath = csDestPath;
		sPath = sPath + strFileName[i] + "//";
		char* sDestPath = const_cast<char*>(sPath.c_str());
		wchar_t* wsDestPath;
		ToWchar(sDestPath, wsDestPath);
		//wcout << wsDestPath << " +++++  " << i << endl;


		int nErr = StartConvert(cvt, wsSrcPath, "");

		if (nErr > 0)
		{
				printf("error !");
		}
		
		//delete all folders before
		int status = delete_dir(sDestPath);
		if(status != 0)
		{
			cout << "remove folder failure!" << endl;
		}

		string imgPath = sPath + "//" + "img";
		string fontPath = sPath + "//" + "font";
		//创建和文件名一样的文件夹
		status = mkdir(sPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		if(status != 0)
		{
			cout << "create folder failure!" << endl;
		}
		//创建存放图片的文件夹
		status = mkdir(imgPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		if(status != 0)
		{
			cout << "create img folder failure!" << endl;
		}
		//创建存放字体的文件夹
		status = mkdir(fontPath.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		if(status != 0)
		{
			cout << "create font folder failure!" << endl;
		}

		cout << "SetDestPath....." << endl;
		SetDestPath(cvt, wsDestPath);

		cout << "GenerateDocDataFile..." << endl;
		GenerateDocDataFile(cvt);

		cout << "GetPageCount..." << endl;
		int nPage = GetPageCount(cvt);
		for (int j=0; j<nPage; j++)
		{
			cout << "GeneratePageDataFile..." << endl;
			GeneratePageDataFile(cvt, j);
		}

		cout << "CloseConvert !!!" << endl;
		CloseConvert(cvt);
		cout << "Exit !!!" << endl;
	}

	DestroyMoudleMgr(mgr);
	return 0;
}
